package com.jd.sunny.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

import com.jd.sunny.exception.ConfigUtilException;
import com.jd.sunny.exception.ExceptionTemplate;
import org.apache.log4j.Logger;


/**
 * 读取配置文件工具类
 *
 * @author :Sun Lin
 * @ClassName:ConfigUtil
 * @Description: java.util.Properties类读取Properties配置文件(完成)
 * @date :2012-9-10
 */
public class ConfigUtil {

    private static final Logger logger = LogUtil.getLogger();

    public static Properties load(String wholeFilePath) throws ConfigUtilException {
        Properties properties = new Properties();
        InputStream inputStream = ConfigUtil.class.getClassLoader().getResourceAsStream(wholeFilePath);
        //加载输入流
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            throw ConfigUtilException.ConfigUtilException_LOAD;
        }
        return properties;
    }

    /**
     * 获取值
     *
     * @param key
     * @return 如果没有，返回null字符串
     * @throws ExceptionTemplate
     * @throws IOException
     */
    public static String getProperty(String wholeFilePath, String key) throws ExceptionTemplate {
        Properties properties;
        String value = "null";
        try {
            properties = load(wholeFilePath);
            //获取属性
            value = properties.getProperty(key);
        } catch (ExceptionTemplate e) {
            throw ConfigUtilException.ConfigUtilException_GET;
        }
        return value;
    }

    /**
     * 通过ResourceBundle方法获取属性值
     *
     * @param wholeFilePath,不需要.properties 后缀
     * @param key
     * @return
     * @throws ExceptionTemplate
     */
    public static String getValue(String wholeFilePath, String key) throws ExceptionTemplate {
        ResourceBundle resourceBundle = ResourceBundle.getBundle(wholeFilePath, Locale.getDefault());
        String value = resourceBundle.getString(key);
        return value;
    }

    /**
     * 获取默认的Common配置文件
     *
     * @param key
     * @return
     * @throws ExceptionTemplate
     */
    public static String getCommonValue(String key) {
        //这儿竟然不要写  resources/whole.properties，不知道为什么
        ResourceBundle wholeResourceBundle = ResourceBundle.getBundle("common", Locale.getDefault());
        String value = wholeResourceBundle.getString(key);
        return value;
    }

    /**
     * 设置Property
     *
     * @param properties
     * @param key
     * @param value
     */
    public static void setProperty(Properties properties, String key, String value) {
        properties.setProperty(key, value);
    }

    /**
     * 保存到指定文件
     *
     * @param targetFile 目标路径
     * @throws ExceptionTemplate
     */
    public static void save(Properties properties, File targetFile) throws ConfigUtilException {
        OutputStream outputStream;
        try {
            outputStream = new FileOutputStream(targetFile);
            properties.store(outputStream, "");
        } catch (FileNotFoundException e) {
            throw ConfigUtilException.ConfigUtilException_SAVE_FileNotFoundException;
        } catch (IOException e) {
            throw ConfigUtilException.ConfigUtilException_SAVE_IOException;
        }
    }

}
